//分页插件
function startPage(className, total, callback, showpage, errorFunction) {
    var page = parseInt($("." + className + " a.active").text()) || 1;
    var showpage = showpage || 5;
    var totalpage = parseInt(total);
    if (page >= totalpage) page = totalpage;
    var star;
    var offset = Math.floor(showpage / 2);
    var end = star + showpage;
    var width = 0;
    var banner = '';
    if (page > 1) {
        banner += "<a>首页</a>";
        banner += "<a>上一页</a>";
    } else {
        banner += "<a class='disabled' >首页</a>";
        banner += "<a class='disabled' >上一页</a>";
    }
    if (totalpage > showpage) {
        if (page > offset + 1) {
            banner += "<span>...</span>";
            star = page - offset;
            end = page + offset;
        } else {
            star = 1;
            end = showpage;
        }
        if (page + offset > totalpage) {
            star = star - (page + offset - totalpage);
            end = totalpage;
        }
    } else {
        star = 1;
        end = totalpage;
    }


    for (var i = star; i <= end; i++) {
        if (i == page) {
            banner += "<a  class='active'>" + i + "</a>";
        } else {
            banner += "<a>" + i + "</a>";
        }
    }
    if (totalpage > showpage && page + offset < totalpage) {
        banner += "<span>...</span>";
    }
    if (totalpage > page) {
        banner += "<a>下一页</a>";
        banner += "<a>尾页</a>";
    } else {
        banner += "<a class='disabled'>下一页</a>";
        banner += "<a class='disabled'>尾页</a>";
    }
    banner += "<span>&nbsp;&nbsp;&nbsp;共" + totalpage + "页, 跳到</span><input type='text'  /><span>页&nbsp;&nbsp;</span><span class='button'>Go</span>";

    $("." + className).empty().append(banner);
    $("." + className + " a").css({
        "text-decoration": "none",
        cursor: "pointer",
        border: "1px #ccc solid",
        color: "#000",
        "border-radius": "2px",
        padding: "4px 8px",
        margin: "0 1px",
        background: "#F2F4F6"
    });
    $("." + className + " a.active").css({color: "#fff", background: "#4E606C"});
    $("." + className + " a.disabled").css({color: "#ccc"});
    $("." + className + " input").css({width: "30px", height: "20px", "text-align": "center"});
    $("." + className + " .button").css({
        background: "#CCCCCC",
        cursor: "pointer",
        display: "inline-block",
        height: "24px",
        width: "24px",
        "line-height": "24px",
        "text-align": "center",
        "border-radius": "2px"
    });
    $("." + className).children().each(function () {
        width += parseInt($(this).outerWidth());
    })
    $("." + className).css({height: "30px", width: (width + showpage * 2 + 10) + "px", margin: "0px auto"});
    callback(page);

    $("." + className + " a").each(function (index, value) {
        $(this).click(function () {
            if (2 <= index) {
                if (index <= (totalpage >= showpage ? showpage : totalpage) + 1) {
                    $(this).addClass('active').siblings().removeClass('active');
                    startPage(className, total, callback, showpage);
                } else {
                    if (index == (totalpage >= showpage ? showpage : totalpage) + 2) {
                        if (!$(this).hasClass('disabled')) {
                            $('.' + className + ' a.active').next().addClass('active').siblings().removeClass('active');
                            startPage(className, total, callback, showpage);
                        }
                    } else {
                        $(this).addClass('active').text(totalpage).siblings().removeClass('active');
                        ;
                        startPage(className, total, callback, showpage);
                    }
                }
            } else {
                if (index == 1) {
                    if (!$(this).hasClass('disabled')) {
                        $('.' + className + ' a.active').prev().addClass('active').siblings().removeClass('active');
                        startPage(className, total, callback, showpage);
                    }
                } else {
                    $(this).addClass('active').text(1).siblings().removeClass('active');
                    ;
                    startPage(className, total, callback, showpage);
                }
            }
        });
    })
    $("." + className + " .button").click(function () {
        var val = $("." + className + " input").val();
        if (val == '' || val <= 0 || val > totalpage) {
            if (errorFunction == null) alert('页码不正确，请重新输入');
            else errorFunction('页码不正确，请重新输入');
        } else {
            $("." + className + " a.active").text(val);
            startPage(className, total, callback, showpage);
        }
    });
}
	